﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom;
using NBusiness.Templates;
using NBusiness.CodeDom.Compiler;
using System.CodeDom.Compiler;
using NBusiness.ESharp.Compiler;

namespace NBusiness.ESharp
{
    [Serializable]
    public class ESharpTemplate : EntityTemplate
    {
        protected override void InitializeAssignmentLogic()
        {
            Assignments.Add(new AssignmentLogic( ESharpEntityElementTypes.EntityTemplateType ));

            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.EntityTemplateTypeJoin,
                new AssignmentLogicHandler(AppendName) ));

            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.EntityTemplateSeparator,
                PostAssignmentAction.Stop ));

            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.GroupBegin,
                PostAssignmentAction.Stop ));

            Assignments.Add(new AssignmentLogic( ESharpEntityElementTypes.WhiteSpace ));
            Assignments.Add(new AssignmentLogic( ESharpEntityElementTypes.Comment ));
        }

        protected override void Attach()
        {
            // Do nothing...
        }

        private void AppendName(object sender, AssignmentLogicEventArgs e)
        {
            EntityElementType type = e.Elements.Peek().Type;
            if (type == ESharpEntityElementTypes.EntityTemplateType)
            {
                Name += e.Elements.Peek().Value;
            }
            else if (type == ESharpEntityElementTypes.EntityTemplateTypeJoin)
            {
                Name += ".";
            }
        }
    }
}
